package nezumi.gui;

import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics2D;
import java.awt.Point;

import nezumi.Constants;
import nezumi.state.State;

public class OccupancyGridEntity extends AEntity {

	public OccupancyGridEntity(State state, Component enclosingComp) {
		super(state, enclosingComp);
	}

	public void draw(Graphics2D g) {

		final double[][] occupancyGrid = state.getMapState().getOccupancyGrid();
		final int rows = state.getMapState().getHeight();
		final int cols = state.getMapState().getWidth();

		final double height = (double) this.canvasSize.height
				/ (Constants.MAP_HEIGHT / Constants.MAP_GRANULARITY * 2);
		final double width = (double) this.canvasSize.width
				/ (Constants.MAP_WIDTH / Constants.MAP_GRANULARITY * 2);

		final int intheight = (int) Math.round(height);
		final int intwidth = (int) Math.round(width);

		Color oldColor = g.getColor();
		for (int i = 0; i < cols; i++)
			for (int j = 0; j < rows; j++) {

				if (occupancyGrid[i][j] <= 0.4) // no wall
					g.setColor(Color.GREEN);
				else if (occupancyGrid[i][j] <= Constants.WALKABLE_THRESHOLD) // unsure
					g.setColor(Color.BLACK);
				else
					g.setColor(Color.RED); // wall

				Point t = this.translatePoint(new Point(i, j));

				g.fillRect((int) Math.round(t.getX() - width / 2), (int) Math
						.round(t.getY() - height / 2), intwidth, intheight);

			}
		g.setColor(oldColor);
	}
}
